package com.example.a2022application;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a2022application.activity.RegisterActivity;
import com.example.a2022application.bean.LoginBean;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.SpUtils;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import androidx.annotation.Nullable;


/**
 * 登录页面
 */
public class MainActivity extends BaseActivity {
    private Activity myActivity;
    MySqliteHelper helper = null;
    private EditText etAccount;//账号
    private EditText etPassword;//密码
    private TextView tvRegister;//注册
    private TextView tvForgotpwd;//忘记密码
    private Button btnLogin;//登录按钮
    private Spinner spIdentity;//身份
    private String getContent;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myActivity = this;
        helper = new MySqliteHelper(this);
        etAccount =(EditText) findViewById(R.id.et_account);//获取账号
        etPassword = (EditText)findViewById(R.id.et_password);//获取密码
        tvRegister = (TextView)findViewById(R.id.tv_register);//获取注册
        tvForgotpwd = (TextView)findViewById(R.id.tv_forgotpwd);//忘记密码
        btnLogin = (Button)findViewById(R.id.btn_login);//获取登录
        spIdentity = (Spinner)findViewById(R.id.spin_identity);//获取登录

        String Identity = SpUtils.getString(MainActivity.this,"spIdentity");
        if (!TextUtils.isEmpty(Identity)){
            if ("学生".equals(Identity)) {
                Intent intent = new Intent(myActivity, login_students.class);
                startActivity(intent);
                finish();
            }else if ("老师".equals(Identity)) {
                Intent intent = new Intent(myActivity, login_teachers.class);
                startActivity(intent);
                finish();
            }else if ("管理员".equals(Identity)) {
                Intent intent = new Intent(myActivity, login_administrator.class);
                startActivity(intent);
                finish();
            }
        }
        spIdentity.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                getContent = myActivity.getResources().getStringArray(R.array.professionals)[arg2];
                Log.d("RegisterActivity", "OnCreate getContent");

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //注册
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到注册页面
                Intent intent = new Intent(myActivity, RegisterActivity.class);
                startActivity(intent);
            }
        });
        //忘记密码
        tvForgotpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到忘记密码页面
                Intent intent = new Intent(myActivity, ForgotpwdActivity.class);
                startActivity(intent);
            }
        });

        //设置点击按钮
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取请求参数
                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();

                if ("".equals(account)) {//账号不能为空
                    Toast.makeText(myActivity, "账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if ("".equals(password)) {//密码为空
                    Toast.makeText(myActivity, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                OkGo.<String>post(Urls.LOGIN)
                        .params("account", account)
                        .params("password",password)
                        .params("spIdentity",getContent)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                Log.e("TAG","登录---"+response.body());
                                try {
                                    LoginBean bean = getGson().fromJson(response.body(), LoginBean.class);
                                    if (bean.getCode()==0){
                                        SpUtils.saveString(MainActivity.this,"account",bean.getData().getAccount());
                                        SpUtils.saveString(MainActivity.this,"spIdentity",bean.getData().getSpIdentity());
                                        SpUtils.saveString(MainActivity.this,"userid",bean.getData().getObjId());
                                        SpUtils.saveString(MainActivity.this,"nickname",bean.getData().getNickname());
                                        if ("学生".equals(bean.getData().getSpIdentity())){
                                            SpUtils.saveString(MainActivity.this,"classname",bean.getData().getClasses());
                                        }
                                        if ("学生".equals(getContent)) {
                                            Intent intent = new Intent(myActivity, login_students.class);
                                            startActivity(intent);
                                            finish();
                                        }else if ("老师".equals(getContent)) {
                                            Intent intent = new Intent(myActivity, login_teachers.class);
                                            startActivity(intent);
                                            finish();
                                        }else if ("管理员".equals(getContent)) {
                                            Intent intent = new Intent(myActivity, login_administrator.class);
                                            startActivity(intent);
                                            finish();
                                        }
                                    }else {
                                        ToastUtil.showToast(bean.getMessage());
                                    }

                                }catch (Exception e){

                                }
                            }
                        });
                Log.d("MainActivity", "OnCreate 111");

            }
        });
    }
}